home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / Acere (PowerPlant, Game) 1.2 / AcereÄ.sit / Acereƒ / Code / CardDragTask.cp < prev    next >
Text File  |  1995-02-05  |  4KB  |  121 lines

  1. //    ================        Demonstration code for Drag and Drop was created by:
  2. //    == CardDragTask.cp ==            gdignard@hookup.net (Gilles Dignard)
  3. //    ================            ⌐1994 Gilles Dignard. All Rights Reserved
  4.  
  5. #include "CardDragTask.h"
  6. #include "CardDeck.h"
  7. #include "CardWell.h"
  8.  
  9. //
  10. // LDragTask gives us general drag functionality and behaviour.  CDDTask exists so
  11. // that we can add behaviour specific to dragging cItemType items.
  12. //
  13. // The behaviour we're most interested in is being able to include a 'PICT' flavor
  14. // in our drag. Using LDragTask allows you to include only one flavor in the drag.
  15. // By subclassing LDragTask and overriding AddFlavors and MakeDragRegion, you can
  16. // (a) include as many flavors of an item as you want and (b) define your own outline
  17. // to replace the generic rectangle that LDragTask provides.
  18. //
  19.  
  20. CardDragTask::CardDragTask (const EventRecord &inEventRecord, PlayingCard *inItem)
  21.     : LDragTask(inEventRecord),
  22.       fItem(inItem)
  23. {    // public
  24. }
  25.  
  26. CardDragTask::~CardDragTask ()
  27. {    // public, virtual
  28. }
  29.  
  30. void
  31. CardDragTask::AddFlavors (DragReference inDragRef)
  32. {    // protected, virtual
  33.     Rect theRect;
  34.     fItem->CalcLocalFrameRect(theRect);
  35.     
  36.     Int32 theWidth = theRect.right - theRect.left;
  37.     Int32 theHeight = theRect.bottom - theRect.top;
  38.     
  39.     //
  40.     // Each drag can contain more than one item, and more than one representation
  41.     // for each item. In this demo, we're only dragging one item at a time, but
  42.     // we would like two representations (flavors) of that item. The first flavor will
  43.     // be the actual data structure that we will need to build a new object just like
  44.     // the current one. The second representation will be a 'PICT' representation so
  45.     // that a clipping file can be kept and viewed at the Finder level.
  46.     //
  47.     // Since the possibility exists of including more than one item, we must specify
  48.     // a unique item reference number for each of our item-specific transactions.
  49.     //
  50.     // The app doesn't support multiple item selections, so we know we only have one
  51.     // item. That makes this part pretty simple.
  52.     //
  53.     ItemReference theItemRef = 1;
  54.     
  55.     //
  56.     // Add the first data flavor (the data structure) to the drag
  57.     //
  58.     CardStruct theFlavorData;
  59.     fItem->FillDataStruct(&theFlavorData);
  60.     ::AddDragItemFlavor(inDragRef, theItemRef, cCardWellType,
  61.                         &theFlavorData, sizeof(theFlavorData), 0L);
  62.     
  63.     //
  64.     // Add the second data flavor (the PICT) to the drag
  65.     //
  66. /*    PicHandle thePicH = fItem->CreatePict();
  67.     ::AddDragItemFlavor(inDragRef, theItemRef, 'PICT',
  68.                         *thePicH, ::GetHandleSize((Handle) thePicH), 0L);
  69.     ::KillPicture(thePicH); */
  70. }
  71.  
  72.  
  73. void CardDragTask::MakeDragRegion( DragReference inDragRef, RgnHandle inDragRegion)
  74. {    // protected, virtual
  75.     //
  76.     // We need to indicate to the drag manager what the outline of our
  77.     // object should be.
  78.     //
  79.     ItemReference theItemRef = 1;
  80.     //
  81.     // Parts of this code are cribbed from LDragTask::AddRectDragItem.
  82.     //
  83.     // MD+DDK pages 2-21 through 2-22 also discusses this concept and give
  84.     // another (similar) code example.
  85.     //
  86.     RgnHandle    outerRgn = ::NewRgn();    // Make region containing item
  87.     //
  88.     // First, create a region containing the 'mask' of our object. We'll
  89.     // again use the CDDItem::DoQDInstructions routine to do the QD work for us.
  90.     //
  91.     ::OpenRgn();
  92.     Rect theGlobalRect;
  93.     fItem->CalcPortFrameRect(theGlobalRect);
  94.     ::LocalToGlobal(&topLeft(theGlobalRect));
  95.     ::LocalToGlobal(&botRight(theGlobalRect));
  96. //    fItem->DoQDInstructions(theGlobalRect);
  97.     ::CloseRgn(outerRgn);
  98.     //
  99.     // Take that 'mask', and turn it into an outline by making a copy,
  100.     // shrinking it my one pixel, and subtracting the shrunken copy from
  101.     // the original.
  102.     //
  103.     // That outline is then added (via the 'UnionRgn') to the outline of
  104.     // the drag as a whole.
  105.     //
  106.     RgnHandle    innerRgn = ::NewRgn();    // Carve out interior of region so
  107.     ::CopyRgn(outerRgn, innerRgn);        //   that it's just a one-pixel thick
  108.     ::InsetRgn(innerRgn, 1, 1);            //   outline of the item rectangle
  109.     ::DiffRgn(outerRgn, innerRgn, outerRgn);
  110.     ::DisposeRgn(innerRgn);
  111.     
  112.                                         // Accumulate this item in our
  113.                                         //   total drag region
  114.     ::UnionRgn(outerRgn, inDragRegion, inDragRegion);
  115.     
  116.                                         // Tell Drag Manager about this item
  117.     ::SetDragItemBounds(inDragRef, theItemRef, &(**outerRgn).rgnBBox);
  118.     
  119.     ::DisposeRgn(outerRgn);
  120. }
  121.